package com.cody.controller.question;

import com.cody.entity.*;
import com.cody.service.*;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.*;

/**
 * @author Cody
 * @date 2021/4/8 - 19:20
 */
@Controller
public class QuestionController {
    @Autowired
    private QuestionService questionService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;

    @RequestMapping("/question/del")
    @ResponseBody
    public Map<String, String> del(@RequestParam(required = false, name = "questionId") int questionId) {
        Map<String, String> map = new HashMap<>();
        boolean flag =     questionService.delQuestionByQuestionId(questionId);
        if(flag){
            map.put("msg","删除成功");
        }
        return map;
    }


    //提交问题    上传图片
    private static String dirPath = "D:/abc";

    @PostMapping("/student/submitQuestion")
    public String submitQuestion(@RequestParam(required = false, name = "subject") String subject,
                                 @RequestParam(required = false, name = "section") String section,
                                 @RequestParam(required = false, name = "teacher") String teacher,
                                 @RequestParam(required = false, name = "content") String content,
                                 @RequestParam(required = false, name = "blFile") MultipartFile blFile, HttpSession session) throws IOException {
        if(blFile.getOriginalFilename()!=""){
            Student student = (Student) session.getAttribute("user");
            String fileName = blFile.getOriginalFilename();
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            fileName = UUID.randomUUID() + suffixName;
            String pathName = dirPath + '/' + fileName;
            File dest = new File(pathName);
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            blFile.transferTo(dest);
            //查询出学生的姓名
            student = studentService.querySingleStudentByStudentNumber(student.getStudentNumber());

            Teacher teacher1 = teacherService.querySingleTeacherByTeacherNumber(teacher);
            Question question = new Question(1, content, student.getStudentNumber(),student.getStudentName(), new Date(), subject, section, teacher, teacher1.getTeacherName(),fileName, pathName, 1, 0);
            questionService.insertQuestion(question);
        }else{
            Student student = (Student) session.getAttribute("user");

            student = studentService.querySingleStudentByStudentNumber(student.getStudentNumber());

            Teacher teacher1 = teacherService.querySingleTeacherByTeacherNumber(teacher);
            Question question = new Question(1, content, student.getStudentNumber(),student.getStudentName(), new Date(), subject, section, teacher, teacher1.getTeacherName(),"-1", "-1", 1, 0);
            questionService.insertQuestion(question);
        }

        return "redirect:/student/index";
    }

    @RequestMapping("/download/{questionId}")
    public void download(@PathVariable("questionId") Integer questionId,
                         HttpServletResponse response) throws IOException {
        Question question1 = questionService.queryQuestionByQuestionId(questionId);
        String pathName = question1.getQuestionPicPath();
        File file = new File(pathName);
        response.reset();
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        response.setContentLength((int) file.length());
        response.setHeader("Content-Disposition", "attachment;filename=" + question1.getQuestionPicName());
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        byte[] buff = new byte[1024];
        OutputStream os = response.getOutputStream();
        int i = 0;
        while ((i = bis.read(buff)) != -1) {
            os.write(buff, 0, i);
            os.flush();
        }
    }

    @RequestMapping("/question/update")
    public String update(@RequestParam(required = false, name = "questionId") String questionId,
                         @RequestParam(required = false, name = "questionText") String questionText,
                         HttpSession session) {

        Question question = questionService.queryQuestionByQuestionId(Integer.parseInt(questionId));
        question.setQuestionText(questionText);
        questionService.updQuestionInfo(question);
        return "redirect:/admin/index";
    }


}
